Page({
  data: {
    category: '公告', // 当前选中的分类
    dataList: [] // 当前选中分类下的数据列表
  },
  onLoad(){
    this.getNoticeList()
  },

  // 选择分类
  selectCategory(e) {
    const category = e.currentTarget.dataset.category;
    this.setData({
      category: category
    });
    if(category == '公告'){
      this.getNoticeList()
    }
    if(category == '狗谱'){
      this.getDogList()
    }

    if(category == '图片'){
      this.getPhotoList()
    }

 
  },
  async getPhotoList(){
    const res =await getApp().$request('/api/getDogPhotoList','POST')
    this.setData({
      dataList:res.data
    })
  },
 async getNoticeList(){
const res =await getApp().$request('/api/getNoticeList','POST')
this.setData({
  dataList:res.data
})
 },
 async getDogList(){
  const res =await getApp().$request('/api/getDogList','POST',{userId:wx.getStorageSync('user').id})
  this.setData({
    dataList:res.data
  })
 },

  // 新增数据
  addData() {
    const category = this.data.category
    if(category == '公告'){
      wx.navigateTo({
        url: '/pages/publish2/publish',
      })
    }
    if(category == '狗谱'){
      wx.navigateTo({
        url: '/pages/addDog/addDog',
      })
    }
    if(category == '图片'){
      wx.showModal({
        title: '请输入狗狗Id',
        content: '',
        editable:true,
      }).then(async r=>{
if(r.confirm && r.content){
  const img = await getApp().$upload('photo')
  const res = await getApp().$request('/api/addDogPhoto','POST',{dogId:r.content,photoUrl:img.data,createdAt:getApp().getNowTime()})
wx.showToast({
  title: '新增成功',
})
this.getPhotoList()
}
      })
    }
  },

  // 编辑数据
  editData(e) {
    const id = e.currentTarget.dataset.id;
    if(this.data.category =='公告'){
      wx.navigateTo({
        url: `/pages/publish2/publish?nid=${id}`
      });
    }
    if(this.data.category =='狗谱'){
      wx.navigateTo({
        url: `/pages/addDog/addDog?dogId=${id}`
      });
    }

    
  },

  // 删除数据
  deleteData(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除这条数据吗？',
    }).then(async r=>{
      if(r.confirm){
        if(this.data.category =='公告'){
          const res =await getApp().$request('/api/delNotice','POST',{id:id})
          this.getNoticeList()
        }

        if(this.data.category =='狗谱'){
          const res =await getApp().$request('/api/delDog','POST',{id:id})
          this.getDogList()
        }

        if(this.data.category =='图片'){
          const res =await getApp().$request('/api/delDogPhoto','POST',{id:id})
          this.getPhotoList()
        }



      }
     
    });
  }
});
